package com.iris.egrant.service.proposal;


import com.iris.egrant.app.utils.IrisStringUtils;
import com.iris.egrant.dao.proposal.ProposalCachedExtendDao;
import com.iris.egrant.dao.proposal.ProposalDao;
import com.iris.egrant.dao.proposal.ProposalExtendDao;
import com.iris.egrant.exception.ServiceException;
import com.iris.egrant.model.formInit.FormBaseLibrary;
import com.iris.egrant.model.proposal.Proposal;
import com.iris.egrant.model.proposal.ProposalCached;
import com.iris.egrant.model.proposal.ProposalCachedExtend;
import com.iris.egrant.model.proposal.ProposalExtend;
import com.iris.egrant.service.formInit.InitializeService;
import com.iris.egrant.utils.XMLHelper;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.dom4j.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


@Service("proposalService")
@Transactional(rollbackFor = {Exception.class})
public class ProposalServiceImpl implements ProposalService {
    /*   88 */   private static final Logger LOGGER = LoggerFactory.getLogger(ProposalServiceImpl.class);

    @Autowired
    private ProposalDao proposalDao;
    @Autowired
    private ProposalExtendDao proposalExtendDao;

    @Autowired
    private InitializeService initializeService;

    @Autowired
    protected ProposalCachedExtendDao proposalCachedExtendDao;

    @Override
    public String saveProduct(Long paramLong1, Long paramLong2, String paramString) {
        return null;
    }

    @Override
    public void genPdf(Long paramLong) {

    }

    public String[] getproductxml(String poscode) {
        /* 1432 */
        String[] productxmlarr = {"", ""};
        /* 1433 */
        String productxml = "";
        /* 1434 */
        String prpxml = NumberUtils.isNumber(poscode) ? getProposalCachedXML(Long.valueOf(Long.parseLong(poscode))) : poscode;
        /* 1435 */
        if (!IrisStringUtils.isNullOrBlank(prpxml)) {
            try {
                /* 1437 */
                Document doc = DocumentHelper.parseText(prpxml);
                /* 1438 */
                Node n = doc.selectSingleNode("//data/zh_products");
                /* 1439 */
                if (n != null) {
                    /* 1440 */
                    Element e = (Element) n;
                    /* 1441 */
                    if (e.hasContent()) {
                        /* 1442 */
                        productxml = productxml + e.asXML();
                        /* 1443 */
                        String fromurl = e.elementTextTrim("FROMURL");
                        /* 1444 */
                        fromurl = IrisStringUtils.isNullOrBlank(fromurl) ? e.elementTextTrim("fromurl") : fromurl;
                        /* 1445 */
                        productxmlarr[1] = fromurl;
                        /* 1446 */
                        productxml = productxmlformat(productxml);
                        /* 1447 */
                        productxmlarr[0] = productxml;
                    }
                }
                /* 1450 */
            } catch (DocumentException doce) {
                /* 1451 */
                LOGGER.error("\n\n============error getproductxml()....", (Throwable) doce);
            }
        }
        /* 1454 */
        return productxmlarr;
    }


    private String productxmlformat(String xml) {
        /* 1393 */
        String result = xml.replace("&lt;strong&gt;", "");
        /* 1394 */
        result = result.replace("&lt;/strong&gt;", "");
        /* 1395 */
        Matcher mat = Pattern.compile("</?[^!].*?>").matcher(result);
        /* 1396 */
        while (mat.find()) {
            /* 1397 */
            String tmpstr = mat.group();
            /* 1398 */
            result = result.replace(tmpstr, tmpstr.toLowerCase());
        }
        /* 1400 */
        Pattern pattern = Pattern.compile("<\\?.+\\?>");
        /* 1401 */
        Matcher m = pattern.matcher(result);
        /* 1402 */
        result = m.replaceAll("");
        /* 1403 */
        Pattern pattern2 = Pattern.compile("</?results.*?>");
        /* 1404 */
        Matcher m1 = pattern2.matcher(result);
        /* 1405 */
        result = m1.replaceAll("");
        /* 1406 */
        result = result.replace("<root>", "").replace("</root>", "");
        /* 1407 */
        result = result.replace("<zh_products>", "").replace("</zh_products>", "");
        /* 1408 */
        result = result.replace("<result>", "<zh_product>").replace("</result>", "</zh_product>");
        /* 1409 */
        result = result.replace("seqno", "seq_no");
        /* 1410 */
        result = result.replace("<title>", "<zh_name>").replace("</title>", "</zh_name>");
        /* 1411 */
        result = result.replace("<authors>", "<author>").replace("</authors>", "</author>");
        /* 1412 */
        result = result.replace("ptype", "type_code");
        /* 1413 */
        result = result.replace("pzhname", "type_name");
        /* 1414 */
        result = result.replace("<source>", "<remark>").replace("</source>", "</remark>");
        /* 1415 */
        result = result.replace("gtype", "indexed");

        /* 1417 */
        return result;
    }

    public String getProposalCachedXML(Long poscode) {
        /* 1422 */
        ProposalCachedExtend prpCachedExtend = proposalCachedExtendDao.getPrpInfo(poscode);
        /* 1423 */
        String pcxml = "";
        /* 1424 */
        if (prpCachedExtend != null) {
            /* 1425 */
            return XMLHelper.parseDoc(XMLHelper.StringToDoc(prpCachedExtend.getPrpXml())).asXML();
        }
        /* 1427 */
        return pcxml;
    }

    @Override
    public Map<String, Object> viewPrp(Map<String, Object> paramMap, Long paramLong) throws ServiceException, UnknownHostException {
        return null;
    }


    public FormBaseLibrary viewPrpByPrpCode(Map<String, Object> params, Long prpCode) throws ServiceException {
        /*  258 */
        params.put("formType", "proposal");
        /*  259 */
//        params.put("psn_code", SecurityUtils.getCurrentUserId());
        /*  260 */
        Proposal prp = (Proposal) this.proposalDao.get(prpCode);
        /*  261 */
        params.put("org_code", prp.getOrgCode());

        /*  263 */
        params.put("actionType", Integer.valueOf(3));

        /*  265 */
        ProposalExtend prpExtendInfo = getPrpExtendInfo(prpCode);

        /*  267 */
        Long grantCode = prp.getGrantCode();
        /*  268 */
        Long subGrantCode = prp.getSubGrantCode();
        /*  269 */
        if (subGrantCode != null) {
            /*  270 */
            grantCode = subGrantCode;

        }
        /*  272 */
        if (prpExtendInfo == null || prpExtendInfo.getPrpXML() == null) {
            /*  273 */
            return null;

        }
        /*  275 */
        String prpXml = XMLHelper.parseDoc(XMLHelper.StringToDoc(prpExtendInfo.getPrpXML())).asXML();
        /*  276 */
        FormBaseLibrary fbl = initializeService.getFormBaseLibraryByGrantId(grantCode, null, prpXml, params);
        /*  277 */
        /*  347 */
        return fbl;

    }

    public Map<String, Object> getPrpLoad(Map<String, Object> params, Long posCode) throws ServiceException, UnknownHostException {
        /*  181 */     params.put("formType", "proposal");
        /*  182 */     params.put("psn_code", "1111");
        /*  183 */     if (params.get("org_code") == null) {
            /*  184 */       params.put("org_code", "22222");
            /*      */     }
        /*  186 */     Long grantCode = (Long)params.get("grant_code");
        /*  187 */     Long subGrantCode = (Long)params.get("sub_grant_code");
        /*  188 */     if (subGrantCode != null) {
            /*  189 */       grantCode = subGrantCode;
            /*      */     }
        /*      */
        /*  192 */     Map<String, Object> resultMap = new HashMap<String, Object>();
        /*  193 */     FormBaseLibrary fbl = null;
        /*      */
        /*  195 */     if (posCode == null) {
            /*  196 */       params.put("actionType", Integer.valueOf(1));
            /*  197 */       fbl = this.initializeService.getFormBaseLibraryByGrantId(grantCode, null, null, params);
            /*      */
            /*  199 */       resultMap.put("statYear", "2024-04-14");
            /*      */     } else {
            /*      */
            /*  202 */       params.put("actionType", Integer.valueOf(2));
//            /*  203 */       ProposalCached pc = this.proposalCachedDao.getProposalCacheByPsnCode(SecurityUtils.getCurrentUserId(), posCode);
//            /*  204 */       if (pc == null) {
//                /*  205 */         return null;
//                /*      */       }
//            /*      */
//            /*  208 */       if (pc.getOrgCode() != null) {
                /*  209 */         params.remove("org_code");
                /*  210 */         params.put("org_code", "222");
//                /*      */       }
//            /*  212 */       if (pc.getPsnCode().longValue() == SecurityUtils.getCurrentUserId().longValue()) {
//                /*  213 */         params.put("psn_code", pc.getPsnCode());
//                /*      */       } else {
                /*  215 */         params.put("psn_code", new Long(0L));
//                /*      */       }
            /*  217 */       ProposalCachedExtend prpCachedExtend = this.proposalCachedExtendDao.getPrpInfo(posCode);
            /*  218 */       fbl = this.initializeService.getFormBaseLibraryByGrantId(grantCode,333L, XMLHelper.parseDoc(XMLHelper.StringToDoc(prpCachedExtend.getPrpXml())).asXML(), params);
            /*  219 */       resultMap.put("pdfStatus", "1");
            /*      */     }
        /*      */
        /*  222 */     resultMap.put("fbl", fbl);
        /*  223 */     resultMap.put("xmlData", fbl.getXmlData());
        /*  224 */     return resultMap;
        /*      */   }

    public Proposal getProposalByCode(Long prpCode) {
        /* 1706 */
        return this.proposalDao.viewProposal(prpCode);

    }

    /*      */   public void setReturnMap(Map<String, Object> returnMap, String message, String prpVersion, String xmlData, Long posCode) {
        /*  699 */     returnMap.put("message", message);
        /*  700 */     returnMap.put("prp_version", prpVersion);
        /*  701 */     returnMap.put("xmlData", xmlData);
        /*  702 */     returnMap.put("pos_code", posCode);
        /*  703 */     returnMap.put("desedPosCode", ObjectUtils.toString(posCode));
        /*      */   }
    /*      */

    public ProposalExtend getPrpExtendInfo(Long prpCode) {
        return this.proposalExtendDao.getPrpExtendInfo(prpCode);
    }
}